package com.web.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.mysql.jdbc.StringUtils;
import com.web.action.res.Response;
import com.web.common.Sys;
import com.web.model.PageBean;
import com.web.model.TUser;
import com.web.model.TWinNum;
import com.web.model.constant.Constant;
import com.web.service.TWinNumService;
import com.web.utils.WebUtil;
import com.web.utils.WinNumUtil;

@Controller
@RequestMapping("win")
public class TWinNumAction {
	
	@Autowired
	private TWinNumService winNumService;
	
	@Resource(name = "winNumUtil")
	private WinNumUtil winNumUtil;
	
	@Resource(name ="sys")
	private Sys sys;
	
	@RequestMapping(value="win_num_list", method = RequestMethod.GET)
	@ResponseBody
	public ModelAndView winNumList(HttpServletRequest request,HttpSession session){
		ModelAndView mv = new ModelAndView();
		TUser sessionUser = (TUser) session.getAttribute(Constant.SESSION_USER);
		if (sessionUser == null) {
			mv.setViewName("redirect:/user/login");
			return mv;
		}
		//获取今天日期
		String date = WebUtil.getStringDate(WebUtil.getDateTimestamp(),Constant.DATA);
		mv.addObject("date", date);
		
		String page = (request.getParameter("page") == null ? "1" : request.getParameter("page"));
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("pageNum", Integer.parseInt(page));
		param.put("pageSize", 20);
		param.put("sort", "create_time");
		PageBean<TWinNum> winNumList = winNumService.selectListByPage(param);

		winNumList.setUrl("/win/win_num_list?page=");
		winNumList.getPageStr();
		mv.addObject("winNumList", winNumList);
		mv.setViewName("/winning");
		return mv;
	}

	/**
	 * 获取下一期开奖结果
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "next_win_num",method = RequestMethod.GET)
	@ResponseBody
	public TWinNum nextWinNum(TWinNum num,HttpServletRequest request,HttpSession session){
		return winNumService.selectByPrimaryKey(num.getTermCd());
	}
	
	@RequestMapping(value="fill_win_num",method = RequestMethod.POST)
	@ResponseBody
	public Response fillWinNum(String winNums,String termcd,String time){
		Response res = new Response();
		if(StringUtils.isNullOrEmpty(winNums.trim()) || StringUtils.isNullOrEmpty(termcd.trim()) || StringUtils.isNullOrEmpty(time.trim())){
			return res.ParamError;
		}
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			TWinNum insertWinNum = new TWinNum();
			String[] numArr = winNums.split(",");
			insertWinNum.setNum1(numArr[0]);
			insertWinNum.setNum2(numArr[1]);
			insertWinNum.setNum3(numArr[2]);
			insertWinNum.setNum4(numArr[3]);
			insertWinNum.setNum5(numArr[4]);
			insertWinNum.setNum6(numArr[5]);
			insertWinNum.setNum7(numArr[6]);
			insertWinNum.setNum8(numArr[7]);
			insertWinNum.setNum9(numArr[8]);
			insertWinNum.setNum10(numArr[9]);
			insertWinNum.setWinTime(sdf.parse(time));
			insertWinNum.setTermCd(Long.valueOf(termcd));
			
			TWinNum rwinnum = winNumService.selectByPrimaryKey(Long.valueOf(termcd));
			if(rwinnum == null){
				winNumService.insert(insertWinNum);
				winNumUtil.checkWinNum(insertWinNum, numArr);
			}
			return res.SUCC; 
		} catch (ParseException e) {
			e.printStackTrace();
			return res.ParamError;
		}
		
		
	}
}
